home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Financial / USDebtWatch / unix-version / xdebt.c < prev    next >
C/C++ Source or Header  |  1995-06-12  |  3KB  |  126 lines

  1. /* Displays the current national debt in a window, updated once a second.
  2.    Public domain.  By Jamie Zawinski <jwz@lucid.com>.
  3.    cc -O -o xdebt xdebt.c -lXaw -lXmu -lXt -lX11
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <X11/Xos.h>
  8. #include <X11/Intrinsic.h>
  9. #include <X11/Label.h>
  10.  
  11. /* The US National Debt, and its rate of increase as of December 31, 1988
  12.    (in dollars per second) according to the 1989 Survey of Current Business.
  13. .  These values are floats because they're >42 bits.
  14.  */
  15. typedef double debt_t;
  16. #define DEBT  2707284000000.0
  17. #define DELTA 7673.015
  18. static struct tm debt_tm = { 0, 0, 0, 31, 11, 88 };
  19.  
  20. debt_t
  21. national_debt_at (now)
  22.      time_t now;
  23. {
  24.   time_t debt_date = timelocal (&debt_tm);
  25.   time_t seconds_since_then = now - debt_date;
  26.   debt_t delta_since_then = DELTA * seconds_since_then;
  27.   return DEBT + delta_since_then;
  28. }
  29.  
  30. extern char *index ();
  31.  
  32. void
  33. debt_to_string (debt, s)
  34.      debt_t debt;
  35.      char *s;
  36. {
  37.   int L, i = 0;
  38.   char buf [255];
  39.   char *b = buf;
  40.   sprintf (b, "%lf", debt);
  41.   b = index (b, '.') - 1;
  42.   L = b - buf;
  43.   s += L + (L / 3);
  44.   *(s+1) = 0;
  45.   i = -1;
  46.   while (1)
  47.     {
  48.       if (++i == 3) i = 0, *s-- = ',';
  49.       if (b < buf) return;
  50.       *s-- = *b--;
  51.     }
  52. }
  53.  
  54.  
  55. static int update;
  56.  
  57. static char *defaults[] = {
  58.   "*Label.font:    *-times-bold-r-*-*-*-240-*-*-*-*-*-*",
  59.   "*title:    Current U.S. National Debt",
  60.   "*update:    1",
  61.   NULL
  62. };
  63.  
  64. static XrmOptionDescRec options [] = {
  65.   { "-update",    "*update",    XrmoptionSepArg, 0 }
  66. };
  67.  
  68.  
  69. static void
  70. get_update (dpy)    /* Easier than making a subclass... */
  71.      Display *dpy;
  72. {
  73.   char *name, *class, *type, buf1[255], buf2[255];
  74.   XrmValue value;
  75.   XtGetApplicationNameAndClass (dpy, &name, &class);
  76.   sprintf (buf1, "%s.update", name);
  77.   sprintf (buf2, "%s.Update", class);
  78.   XrmGetResource (XtDatabase (dpy), buf1, buf2, &type, &value);
  79.   if (sscanf (value.addr, " %d ", &update) == 0 || update < 1)
  80.     {
  81.       fprintf (stderr, "%s: update must be a positive integer, not \"%s\"\n",
  82.            name, value.addr);
  83.       update = 1;
  84.     }
  85. }
  86.  
  87.  
  88. static void
  89. timer (w, id)
  90.      Widget w;
  91.      XtIntervalId id;
  92. {
  93.   char buf [255];
  94.   Arg av [10];
  95.   int ac = 0;
  96.   buf [0] = '$';
  97.   debt_to_string (national_debt_at (time ((time_t *) 0)), buf + 1);
  98.   XtSetArg (av [ac], XtNlabel, buf); ac++;
  99.   XtSetValues (w, av, ac);
  100.   XtAppAddTimeOut (XtWidgetToApplicationContext (w), update * 1000, timer, w);
  101. }
  102.  
  103.  
  104. void
  105. main (argc, argv)
  106.      int argc;
  107.      char **argv;
  108. {
  109.   XtAppContext app;
  110.   Widget shell = XtAppInitialize (&app, "XDebt", options, XtNumber (options),
  111.                   &argc, argv, defaults, NULL, 0);
  112.   Widget label;
  113.   XEvent event;
  114.   if (argc > 1)
  115.     {
  116.       fprintf (stderr, "%s: unknown option %s\n", argv[0], argv[1]);
  117.       fprintf (stderr, "options: -update <seconds>\n\t -font <font>\n");
  118.       exit (-1);
  119.     }
  120.   label = XtCreateManagedWidget ("label", labelWidgetClass, shell, NULL, 0);
  121.   get_update (XtDisplay (label));
  122.   timer (label, 0);
  123.   XtRealizeWidget (shell);
  124.   XtAppMainLoop (app);
  125. }
  126.